package com.leyou.search.listeners;


import com.leyou.search.service.IndexService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component //组件
public class Listeners {//监听器 用来监听mq上传来的信息

    @Autowired
    private IndexService indexService;
    //声明绑定关系
    //声明队列,队列绑定给交换机,绑定时候要写绑定的routingKey
    @RabbitListener(bindings = @QueueBinding(//item.up.queue队列  leyoujhj交换机 item.uproutingKey
            value = @Queue(name = "item.up.queue" ,durable = "true"),
            exchange = @Exchange(name = "leyoujhj", type = ExchangeTypes.TOPIC),
            key = "item.up"))
    public void up(Long id){ //id为ma传来的信息

        //创建索引
        this.indexService.crateIndex(id); //此方法通过mq传来的id,进行商品上架之后的更新
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "item.up.queue" ,durable = "true"),
            exchange = @Exchange(name = "leyoujhj", type = ExchangeTypes.TOPIC),
            key = "item.down"))
    public void down(Long id){

        this.indexService.deteleIndex(id); //此方法通过mq传来的id ,进行商品下架之后的删除,(索引库删除)
    }


}
